package edu.buu.refectory.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * MD5工具类.
 *
 * @author Henley<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2016-01-18 <br>
 */
public class MD5Util {

    /**
     * Encrypt.
     *
     * @param password the password
     * @return the string
     */
    private static String encrypt(String password) {
        if (password == null || password.length() == 0) {
            throw new IllegalArgumentException("String to encrypt cannot be null or zero length");
        }
        StringBuffer hexString = new StringBuffer();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(password.getBytes());
            byte[] hash = md.digest();

            for (int i = 0; i < hash.length; i++) {
                if ((0xff & hash[i]) < 0x10) {
                    hexString.append("0"
                            + Integer.toHexString((0xFF & hash[i])));
                } else {
                    hexString.append(Integer.toHexString(0xFF & hash[i]));
                }
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return hexString.toString();
    }
    
    /**
     * 采用md5加密，同时添加盐.
     *
     * @param password the password
     * @param salt the salt
     * @return the string
     */
    public static String encrypt(String password, String salt) {
        return encrypt(password + "{" + salt + "}");
    }
    
    /**
     * The main method.
     *
     * @param str the arguments
     */
    public static void main(String[]str){
       System.out.println(UUID.randomUUID().toString().replace("-", ""));
       
       
       System.out.println(encrypt("000000", "henley"));
    }
	
}
